/*
Copyright (c) 2010, Jonathan Baldwin
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
  list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this
  list of conditions and the following disclaimer in the documentation and/or
  other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "FrameQueue.h"
#include <cstring>

namespace recap
{
FrameQueue::Reader::Reader(FrameQueue& buffer) : m_buffer(&buffer)
{
}
FrameQueue::Reader::Reader(const recap::FrameQueue::Reader& other)
{
    m_buffer = other.m_buffer;
}
FrameQueue::Reader& FrameQueue::Reader::operator=(const Reader& other)
{
    m_buffer = other.m_buffer;
    return *this;
}
FrameQueue::Reader::operator const char*() const
{
    return m_buffer->get_read_pointer();
}
const char& FrameQueue::Reader::operator[](unsigned int index) const
{
    return m_buffer->get_read_pointer()[index];
}
FrameQueue::Reader& FrameQueue::Reader::operator++()
{
    m_buffer->increment_read_pointer();
    return *this;
}
void FrameQueue::Reader::operator++(int ) const
{
    m_buffer->increment_read_pointer();
}
unsigned int FrameQueue::Reader::frameSize() const
{
  return m_buffer->frameSize();
}
bool FrameQueue::Reader::hasQueuedData() const
{
  return m_buffer->hasQueuedData();
}


FrameQueue::Writer::Writer(FrameQueue& buffer) : m_buffer(&buffer)
{
}
FrameQueue::Writer::Writer(const recap::FrameQueue::Writer& other) : m_buffer(other.m_buffer)
{
}
FrameQueue::Writer& FrameQueue::Writer::operator=(const recap::FrameQueue::Writer& other)
{
    m_buffer = other.m_buffer;
}
FrameQueue::Writer::operator char*() const
{
    return m_buffer->get_write_pointer();
}
char& FrameQueue::Writer::operator[](unsigned int index) const
{
    return m_buffer->get_write_pointer()[index];
}
FrameQueue::Writer& FrameQueue::Writer::operator++()
{
    m_buffer->increment_write_pointer();
    return *this;
}
void FrameQueue::Writer::operator++(int ) const
{
    m_buffer->increment_write_pointer();
}
unsigned int FrameQueue::Writer::frameSize() const
{
  return m_buffer->frameSize();
}


FrameQueue::FrameQueue(unsigned int frameSize) : m_read_frame_number(0),m_write_frame_number(0),m_frame_size(frameSize)
{
}
FrameQueue::~FrameQueue()
{
    QWriteLocker locker(&m_lock);
    while (!m_data_array.isEmpty())
        delete[] m_data_array.take(m_data_array.keys().first());
}

FrameQueue::Reader FrameQueue::reader()
{
    return Reader(*this);
}
FrameQueue::Writer FrameQueue::writer()
{
    return Writer(*this);
}
unsigned int FrameQueue::readFrameNumber() const
{
    QReadLocker locker(&m_lock);
    return m_read_frame_number;
}
unsigned int FrameQueue::writeFrameNumber() const
{
    QReadLocker locker(&m_lock);
    return m_write_frame_number;
}
unsigned int FrameQueue::frameSize() const
{
  return m_frame_size;
}
void FrameQueue::setFrameSize(unsigned int frameSize)
{
  m_frame_size = frameSize;
}
bool FrameQueue::hasQueuedData() const
{
    QReadLocker locker(&m_lock);
    return m_read_frame_number < m_write_frame_number;
}
void FrameQueue::clear()
{
  QWriteLocker locker(&m_lock);
  
    while (!m_data_array.isEmpty())
        delete[] m_data_array.take(m_data_array.keys().first());
    m_read_frame_number = m_write_frame_number = 0;
}


char* FrameQueue::get_read_pointer()
{
    QReadLocker locker(&m_lock);
    return m_data_array[m_read_frame_number];
}
char* FrameQueue::get_write_pointer()
{
    QReadLocker locker(&m_lock);
    return m_data_array[m_write_frame_number];
}
char* FrameQueue::get_pointer(unsigned int queued_frame)
{
    QReadLocker locker(&m_lock);
    if (m_write_frame_number<(m_read_frame_number+queued_frame))
        qFatal("Tried to read a frame that doesn't yet exist");
    return m_data_array[m_read_frame_number+queued_frame];
}
void FrameQueue::increment_read_pointer()
{
    QWriteLocker locker(&m_lock);
    if (m_read_frame_number >= m_write_frame_number)
        qFatal("Tried to increment read pointer past write pointer");
    //delete m_data_array[];
    delete[] m_data_array.take(m_read_frame_number);
    m_read_frame_number++;
}
void FrameQueue::increment_write_pointer()
{
    QWriteLocker locker(&m_lock);
    m_write_frame_number++;
    m_data_array[m_write_frame_number] = new char[m_frame_size];
}

FrameQueueDevice::FrameQueueDevice(FrameQueue& fq, QObject* parent): QIODevice(parent), m_reader(fq.reader()), m_writer(fq.writer()), m_wOffset(0)
{
}

qint64 FrameQueueDevice::readData(char* data, qint64 maxlen)
{
  qint64 bytes_read = 0;
  while(m_reader.hasQueuedData() && m_reader.frameSize()<= maxlen)
  {
    m_reader++;
    std::memcpy(data, m_reader, m_reader.frameSize());
    data += m_reader.frameSize();
    maxlen -= m_reader.frameSize();
    bytes_read += m_reader.frameSize();
  }
  return bytes_read;
}
qint64 FrameQueueDevice::writeData(const char* data, qint64 len)
{
  qint64 bytes_written =0, write_size;
  
  while(len>0)
  {
    write_size = std::min<qint64>(m_writer.frameSize()-m_wOffset, len);
    if(!m_wOffset)
    m_writer++;
    std::memcpy(((char*)m_writer)+m_wOffset, data, write_size);
    data += write_size;
    len -= write_size;
    m_wOffset = (m_wOffset+write_size)%m_writer.frameSize();
    bytes_written += write_size;
  }
  return bytes_written;
}



}
